﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.IDAL;
using ECCIT;
using OgilvyOne.UKNow.Mobile.Model;
using OgilvyOne.UKNow.Mobile.Model.Events;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.DALFactory;
using ECCIT.Extension.Weibo_v2.Common;
using OgilvyOne.UKNow.Mobile.Utilities;
using OgilvyOne.UKNow.Configuration;

namespace OgilvyOne.UKNow.Mobile.APIs.Events
{
   public   class GetListCityIDArtIDandDate:IHttpHandler
    {
        public bool IsReusable
        {
            get { throw new NotImplementedException(); }
        }

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                int pagesize = 10;
                int currindex = 0;
                if (context.Request.Params["page_size"] != null)
                {
                    pagesize = int.Parse(context.Request.Params["page_size"].ToString());
                }

                if (context.Request.Params["current_page"] != null)
                {
                    if (int.Parse(context.Request.Params["current_page"].ToString()) > 0)
                    {
                        currindex = int.Parse(context.Request.Params["current_page"].ToString()) - 1;
                    }
                }

                PagingParameter pagpar = PagingParameterFactory.CreateInstance(true, currindex, pagesize, null);
                if (context.Request.Params["city"] != null && context.Request.Params["art"] != null)
                {
                    GetEvents(Convert.ToInt32(context.Request.Params["city"]), Convert.ToInt32(context.Request.Params["art"]), context, pagpar);
                }
                else if (context.Request.Params["city"] != null && context.Request.Params["date"] != null)
                {
                    GetEvents(Convert.ToInt32(context.Request.Params["city"]), context.Request.Params["date"], context, pagpar);
                }
                else if (context.Request.Params["city"] != null)
                {
                    GetEvents(Convert.ToInt32(context.Request.Params["city"]), context, pagpar);
                }
            }
            catch (Exception)
            {
                context.Response.Write(ResultUtility.ReturnResultJson("-500", "", "获取失败"));
            }

            //throw new NotImplementedException();
            
        }
        private void GetEvents(int cityid, HttpContext htc, PagingParameter pagpar)
        {

            IDALEvents use = EventsDALFactory.Create(2052);
            PagingList plst = use.GetEventsByCityIDPhoneInterface(cityid, pagpar);
            ToWrite(htc, plst, pagpar);

        }
        private void GetEvents(int cityid, int artid, HttpContext htc, PagingParameter pagpar)
        {
            IDALEvents use = EventsDALFactory.Create(2052);
            PagingList plst = use.GetEventsByCityIDAndArtIDPhoneInterface(cityid, artid, pagpar);
            ToWrite(htc, plst, pagpar);
        }
        private void GetEvents( int cityid,string yymm, HttpContext htc, PagingParameter pagpar)
        {
            IDALEvents use = EventsDALFactory.Create(2052);
            PagingList plst = use.GetEventsByCityAndDatePhoneInterface(cityid, yymm.Replace("/", "-"), pagpar);
            ToWrite(htc, plst, pagpar);

        }
        private void ToWrite(HttpContext htc, PagingList plst, PagingParameter pagingParams)
        {
            if (plst.Capacity > 0)
            {
                List<EventInfo> lelc = new List<EventInfo>();
                ModelList<EventInfo> elacdl = new ModelList<EventInfo>();

                elacdl.List = toList(plst, lelc);
                elacdl.CurrentPageIndex = pagingParams.CurrentPageIndex + 1;
                elacdl.TotalCount = plst.Capacity;
                elacdl.PageCount = (plst.Capacity%pagingParams.PageSize == 0)
                                       ? plst.Capacity/pagingParams.PageSize
                                       : plst.Capacity/pagingParams.PageSize + 1;
                string s = JsonSerializationHelper.ObjectToJson(elacdl).Replace("null", "\"\"");
                htc.Response.Write(ResultUtility.ReturnResultJson("0", s, "获取成功"));
            }
            else
            {
                htc.Response.Write(ResultUtility.ReturnResultJson("0", "", "没有数据"));
            }
        }

        private List<EventInfo> toList(PagingList plst, List<EventInfo> lelc)
        {
            APIComm acm=new APIComm ();
            foreach (EventsModel em in plst.List)
            {
                EventInfo elac = ModelConvertUtility.ToEventInfo(em, false);
                lelc.Add(elac);
            }
            return lelc;
        }
    }
}
